<?php
namespace Home\Controller;

/**
 * RankController
 * 榜单
 */
class RankController extends CommonController {
    /**
     * 榜单列表
     * @return
     */
    public function index() {
        $result = $this->getPagination('Rank',['is_del'=>0]);

        foreach($result['data'] as $key => $val){
            $result['data'][$key]['rank_novel_number'] = M('novelrank')->where(['rank_id'=>$val['id'],'is_del'=>0])->count();
        }

        $this->assign('list', $result['data']);
        $this->assign('rows_count', $result['total_rows']);
        $this->assign('page', $result['show']);
        $this->display();
    }

    /**
     * 添加榜单页面
     */
    public function add(){
        $this->display();
    }

    /**
     * 添加榜单操作
     */
    public function insert(){
        $rank_Model = M('rank');
        $data = I('post.data');
        if (!isset($data)) {
            return $this->errorReturn('无效的操作！');
        }

        if($data['rank_name'] == '' || !$data['rank_name']){
            return $this->errorReturn('榜单名称不能为空！');
        }

        //上传榜单图片
        if($_FILES['pic']['tmp_name']){
            $info = $this->imgUpload('rank_pic');
            if(!$info){
                return $this->errorReturn('榜单图片上传失败请重试！');
            }else{
                $data['pic'] = '/Public/uploads/'.$info['pic']['savepath'].$info['pic']['savename'];
            }
        }

        $result = $rank_Model->add($data);
        if (!$result) {
            return $this->errorReturn('添加失败，稍后请重试！');
        }

        return $this->successReturn('添加榜单成功！', U('Rank/index'));
    }


    /**
     * 编辑榜单页面
     * @return
     */
    public function edit() {
        if (!isset($_GET['id'])) {
            return $this->error('参数错误，稍后请重试！');
        }

        //榜单详情
        $rank = M('rank')->getById($_GET['id']);

        $this->assign('rank', $rank);

        $this->assign('id',$_GET['id']);
        $this->display();
    }

    /**
     * 更新榜单信息
     * @return
     */
    public function update() {
        $rank_Model = M('Rank');
        $data = I('post.data');
        if(!isset($data)){
            return $this->errorReturn('无效的操作！');
        }

        if($data['rank_name'] == '' || !$data['rank_name']){
            return $this->errorReturn('榜单名称不能为空！');
        }

        $data['update_time'] = time();

        //上传封面
        if($_FILES['pic']['tmp_name']){
            $info = $this->imgUpload('rank_pic');
            if(!$info){
                return $this->errorReturn('封面上传失败请重试！');
            }else{
                $data['pic'] = '/Public/uploads/'.$info['pic']['savepath'].$info['pic']['savename'];
            }
        }

        $result = $rank_Model->where(['id'=>$data['id']])->save($data);
        if (!$result) {
            return $this->errorReturn('操作失败，请重试！');
        }

        return $this->successReturn('更新榜单信息成功！', U('Rank/index'));
    }

    /**
     * 删除榜单
     */
    public function del(){
        $rank_Model = M('rank');
        $id = I('post.id');
        if(!$id){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数错误，请重试']);
        }

        $result = $rank_Model->where(['id'=>$id])->setField('is_del',1);
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'删除成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'删除失败，请重试!']);
        }
    }

    /**
     * 将小说从对应榜单中移除
     */
    public function remove_rank(){
        $novel_id = I('post.novel_id');
        $rank_id = I('post.rank_id');

        $rank_novel_Model = M('novelrank');
        if(!isset($novel_id) || !isset($rank_id)){
            return $this->errorReturn('参数有误！');
        }

        $result = $rank_novel_Model->where(['rank_id'=>$rank_id,'novel_id'=>$novel_id])->delete();
        if($result){
            $this->ajaxReturn(['code'=>0,'msg'=>'移出榜单成功!']);
        }else{
            $this->ajaxReturn(['code'=>-1,'msg'=>'移出榜单失败，请重试!']);
        }
    }

    /**
     * 将小说添加到排行榜
     */
    public function add_rank_novel(){
        $novel_rank_Model = M('novelrank');
        $novel_id = I('post.novel_id');
        $rank_id = I('post.rank_id');

        $data['novel_id'] = $novel_id;
        $data['rank_id'] = $rank_id;

        if($novel_id == 0 || $rank_id == 0 || !isset($novel_id) || !isset($rank_id)){
            $this->ajaxReturn(['code'=>-1,'msg'=>'参数有误，请重试!']);
        }
        $is_rank = $novel_rank_Model->where(['rank_id'=>$data['rank_id'],'novel_id'=>$data['novel_id']])->find();
        if($is_rank){
            $this->ajaxReturn(['code'=>-1,'msg'=>'此小说已经在此榜单中!']);
        }else{
            $result = $novel_rank_Model->add($data);
            if($result){
                $this->ajaxReturn(['code'=>0,'msg'=>'添加榜单成功!']);
            }else{
                $this->ajaxReturn(['code'=>-1,'msg'=>'添加失败，请重试']);
            }
        }

    }


}
